/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package adee.qrlogin;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;



/**
 *
 * @author adee
 */
public class AccountFormActivity extends Activity {
    private Account account;
    private AccountsDB db;
    private EditText username_f;
    private EditText password_f;
    private EditText link_f;
    private Context context;
    private LoginInfo loginInfo;
    private AccountForm accountForm;


    private void buildForm(){
        
    }
    private void loadAccount(){
        Bundle extras = this.getIntent().getExtras();
        if (extras!=null){
            if (extras.containsKey("id")){
                this.account = db.getAccount(Integer.parseInt(extras.getString("id")));
            } 

            String link=null,sessionId=null;
            if (extras.containsKey("link")){
                link=extras.getString("link");
            }

            if (extras.containsKey("sessionid")){
                sessionId= extras.getString("sessionid");
            }

            if( (link!=null) && (sessionId!=null))
            this.account = new Account(link, sessionId);
        }


    }

    private OnClickListener save = new OnClickListener() {


        public void onClick(View view) {
          
              accountForm.updateAccount();
              db.saveAccount(account);
              Activity a = (Activity)context;
              a.setResult(RESULT_OK,a.getIntent());
              a.getIntent().putExtra("id",String.valueOf(account.getId()));
              a.finish();
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.account_form);
        this.db=new AccountsDB(this);
        this.context = this;
        this.loadAccount();
        Button save_b = (Button) findViewById(R.id.save_account_button);
        save_b.setOnClickListener(save);

        LinearLayout layout = (LinearLayout) findViewById(R.id.account_form_main_layout);
        this.accountForm = new AccountForm(account, context);
        layout.addView(this.accountForm.getLayout());
    }


}
